//
//  SLDeployViewModel.m
//  StarLight
//
//  Created by Duke.wu on 16/5/30.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLDeployViewModel.h"
#import "SLNetWork.h"



@interface SLDeployViewModel ()

@property (nonatomic, strong) NSNumber *pageNum;

@property (nonatomic, strong) NSString *requestPath;

@property (nonatomic, assign) DeployViewModelType type;

@end

@implementation SLDeployViewModel

- (instancetype)initWithType:(DeployViewModelType)type{
  if (self = [super init]) {
    self.type = type;
    self.pageNum = @1;
    [self addObservers];
  }
  return self;
}

- (void)addObservers{
  
  @weakify(self)
  
  [[RACObserve(self, requestPath) filter:^BOOL(id value) {
    return value != nil;
  }] subscribeNext:^(NSString *x) {
    @strongify(self)
    [self getdataWith:x];
  }];
  
  [RACObserve(self, pageNum) subscribeNext:^(NSNumber *page) {
    @strongify(self)
    [self changeRequstWithPage:page];
  }];
  
  
}


#pragma mark - Private Methods

- (void)changeRequstWithPage:(NSNumber *)page{
  NSDictionary *dic = [DEFAULTS objectForKey:@"loginInformation"];
  NSString *request;
  
  if (self.type == DeployViewModelTypeGoodOwner) {
    request = [NSString stringWithFormat:@"%@/goods/mygoods?uid=%@&page=%@",baseUrl,dic[@"id"],page];
  }else if(self.type == DeployViewModelTypeShipOwner){
    request = [NSString stringWithFormat:@"%@/boat/boatlist?uid=%@",baseUrl,dic[@"id"]];
  }
  self.requestPath = request;
}

- (void)getdataWith:(NSString *)requst{
  
  [SLNetWork getDataWithRequest:requst complete:^(id respond, NSError *error) {
    NSMutableArray *dataArr = [NSMutableArray arrayWithCapacity:1];
   
    
    if (![self.pageNum isEqualToNumber:@1]) {
      [dataArr addObjectsFromArray:self.dataSource];
    }

    if (!error) {
      NSMutableArray * arr;
      if (self.type == DeployViewModelTypeGoodOwner) {
        arr = [SLDeployGood mj_objectArrayWithKeyValuesArray:respond[@"data"]];
      }else{
        arr = [SLDeployShip mj_objectArrayWithKeyValuesArray:respond[@"data"]];
      }
      [dataArr addObjectsFromArray:arr];
      
      self.dataSource = dataArr;
    }
  }];
}



#pragma mark - Public Methods

- (void)refreshGoodsWith:(NSInteger)state gid:(NSString *)gid{
  NSDictionary *para = @{@"hzid":[SLNetWork backUid], @"gid":gid,@"state":@(state)};
  NSString *postUrl = [NSString backUrlWithService:@"goods" methodName:@"refmygoods"];
  @weakify(self)
  
  [SVProgressHUD showWithStatus:@"正在更新"];
  [SLNetWork postDataWithParameters:para postUrl:postUrl complete:^(id respond, NSError *error) {
    if (!error) {
     @strongify(self)
      [self first];
    }
  }];
  
}


- (void)next{
  self.pageNum = @(self.pageNum.integerValue +1);
}

- (void)first{
  self.pageNum = @1;
}



@end


@implementation SLDeployShip

//+ (NSDictionary *)mj_replacedKeyFromPropertyName{
//  return @{@"bid":@"id"};
//}

@end



@implementation SLDeployGood

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
  return @{@"gid":@"id"};
}

@end






